#!/bin/bash
set -e

REVISIONNUMBER=`hg log --template '{rev}\n' -r -1`
HGGCPASSWORD=`grep wikignpi.password ~/.hgrc | sed 's/wikignpi.password = //'`

function packem()
{
  for a in ${THEFILES}; do cp -r $a gadgets/; done
  zip -r -9 releases/${THEZIPFILE} gadgets
  ./googlecode_upload.py -s "${1}" -p wikignpi -u wyldckat -w ${HGGCPASSWORD} -l "${2}" releases/${THEZIPFILE}
  for a in ${THEFILES}; do rm -rf gadgets/$a; done
}

if [ ! -d "releases" ]; then mkdir releases; fi
if [ ! -d "gadgets" ]; then mkdir gadgets; fi

#stage area - basic files
cp ph_core.min.css ph_detail.min.css d_ie.css xregexp-min.js GoogleCodeWikiParser.min.js gadgets/

#package simple gadget wiki
THEZIPFILE="wikignpi_simplewikigateway_r${REVISIONNUMBER}.zip"
THEFILES="simplewikigateway.html simplewikigateway.xml"
packem "Revision ${REVISIONNUMBER} for simplewikigateway." "simplewikigateway,gadget"

#package print kit
THEZIPFILE="wikignpi_printwikis_r${REVISIONNUMBER}.zip"
THEFILES="printwikis.html printwikis-min.js"
packem "Revision ${REVISIONNUMBER} for Print It (printwikis)." "printwikis,print"

#package dynamic gadget kit
THEZIPFILE="wikignpi_wikigateway_r${REVISIONNUMBER}.zip"
THEFILES="wikigateway.xml wikigateway-min.js plugins"
packem "Revision ${REVISIONNUMBER} for Gadget It (wikigateway)." "wikigateway,gadget,plugin"

#package preview/edit kit
THEZIPFILE="wikignpi_previewwikis_r${REVISIONNUMBER}.zip"
THEFILES="GoCoWi-previewer.css wikisyntax.css jquery-preview-ed.html"
packem "Revision ${REVISIONNUMBER} for Offline/Local Previewer/Editor." "preview,edit"

#package normal kit
THEZIPFILE="wikignpi_r${REVISIONNUMBER}.zip"
THEFILES="wikigateway.xml wikigateway-min.js printwikis.html printwikis-min.js plugins"
packem "Revision ${REVISIONNUMBER} for Gadget N' Print It (wikigateway + printwikis)." "wikigateway,gadget,plugin,printwikis,print"

#package them all
THEZIPFILE="wikignpi_all_r${REVISIONNUMBER}.zip"
THEFILES="simplewikigateway.html simplewikigateway.xml wikigateway.xml wikigateway-min.js plugins printwikis.html printwikis-min.js xhtmlgateway.xml xhtmlgateway-min.js GoCoWi-previewer.css wikisyntax.css jquery-preview-ed.html"
packem "Revision ${REVISIONNUMBER} for all packages of Gadget N' Print It (wikigateway + printwikis + simplewikigateway + xhtmlgateway)." "wikigateway,gadget,plugin,printwikis,print,simplewikigateway,xhtmlgateway,preview,edit"

#finally clean up the staging area
rm gadgets/*

#These packages do not depend on wiki stuff
#package embed xhtml gadget kit
THEZIPFILE="wikignpi_xhtmlgateway_r${REVISIONNUMBER}.zip"
THEFILES="xhtmlgateway.xml xhtmlgateway-min.js xhtmlplugins xregexp-min.js"
packem "Revision ${REVISIONNUMBER} for XHTML Embed It (xhtmlgateway)." "xhtmlgateway,gadget,plugin"


#now rectify the HowToInstall.wiki page
sed -i -e 's=r[0-9]*\.zip=r'${REVISIONNUMBER}'.zip=g' -e 's=\(^RELEASE DATE: \).*$=\1'"`date --utc`"'=' ../wikignpi-wiki/HowToInstall.wiki

echo "Don't forget to commit+pull the wiki repo!"
